package com.kaikeba.controller;

import com.kaikeba.bean.BootstrapTableExpress;
import com.kaikeba.bean.Express;
import com.kaikeba.bean.Message;
import com.kaikeba.bean.ResultList;
import com.kaikeba.mvc.ResponseBody;
import com.kaikeba.service.ExpressService;
import com.kaikeba.util.DateFormatUtil;
import com.kaikeba.util.JSONUtil;
import com.kaikeba.util.UserUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ExpressController {

    @ResponseBody("/admin/express/Console.do")
    public String console(HttpServletRequest req, HttpServletResponse resp){
        List<Map<String, Integer>> result = ExpressService.console();
        Message msg = new Message();
        if(result == null){
            msg.setStatus(-1);
        }else{
            msg.setStatus(0);
        }
        msg.setObject(result);
        return JSONUtil.toJson(msg);
    }

    @ResponseBody("/admin/express/list.do")
    public String list(HttpServletRequest req, HttpServletResponse resp){
        int offset = Integer.parseInt(req.getParameter("offset"));
        int pageNumber = Integer.parseInt(req.getParameter("pageNumber"));
        List<Express> all = ExpressService.findAll(1, offset, pageNumber);
        List<BootstrapTableExpress> all2 = new ArrayList<>();
        for (Express e : all) {
            BootstrapTableExpress e1 = new BootstrapTableExpress();
            e1.setId(e.getId());
            e1.setNumber(e.getNumber());
            e1.setUsername(e.getUsername());
            e1.setUserPhone(e.getUserPhone());
            e1.setCompany(e.getCompany());
            e1.setCode(e.getCode());
            e1.setInTime(DateFormatUtil.format(e.getInTime()));
            e1.setOutTime(e.getOutTime()==null?"未取件":DateFormatUtil.format(e.getOutTime()));
            e1.setStatus(e.getStatus()==0?"未取件":"已取件");
            e1.setSysPhone(e.getSysPhone());
            all2.add(e1);
        }
        ResultList<BootstrapTableExpress> resultList = new ResultList<>();
        resultList.setRows(all2);
        List<Map<String, Integer>> console = ExpressService.console();
        resultList.setTotal(console.get(0).get("size"));
        return JSONUtil.toJson(resultList);
    }

    @ResponseBody("/admin/express/insert.do")
    public String insert(HttpServletRequest req, HttpServletResponse resp){
        String number = req.getParameter("number");
        String company = req.getParameter("company");
        String username = req.getParameter("username");
        String userPhone = req.getParameter("userPhone");
        Express e = new Express();
        e.setNumber(number);
        e.setCompany(company);
        e.setUsername(username);
        e.setUserPhone(userPhone);
        e.setSysPhone(UserUtil.getUserPhone(req.getSession()));
        boolean result = ExpressService.insert(e);
        Message msg = new Message();
        if(result){
            msg.setStatus(1);
            msg.setResult("添加成功");
        }else{
            msg.setStatus(-1);
            msg.setResult("添加失败");
        }
        return JSONUtil.toJson(msg);
    }

    @ResponseBody("/admin/express/findByNumber.do")
    public String findByNumber(HttpServletRequest req, HttpServletResponse resp){
        String number = req.getParameter("number");
        Express e = ExpressService.findByNumber(number);
        Message msg = new Message();
        if(e == null){
            msg.setStatus(-1);
            msg.setResult("快递不存在");
        }else{
            msg.setStatus(1);
            msg.setResult("查询成功");
            msg.setObject(e);
        }
        return JSONUtil.toJson(msg);
    }

    @ResponseBody("/admin/express/delete.do")
    public String deleteByNumber(HttpServletRequest req, HttpServletResponse resp){
        String number = req.getParameter("number");
        Express e = ExpressService.findByNumber(number);
        boolean result = ExpressService.delete(e.getId());
        Message msg = new Message();
        if(result){
            msg.setStatus(1);
            msg.setResult("删除成功");
        }else{
            msg.setStatus(-1);
            msg.setResult("删除失败");
        }
        return JSONUtil.toJson(msg);
    }

    @ResponseBody("/admin/express/update.do")
    public String updateByNumber(HttpServletRequest req, HttpServletResponse resp){
        String number = req.getParameter("number");
        Express e = ExpressService.findByNumber(number);
        e.setUsername(req.getParameter("username"));
        e.setCompany(req.getParameter("company"));
        e.setUserPhone(req.getParameter("userPhone"));
        e.setStatus(Integer.parseInt(req.getParameter("status")));
        boolean result = ExpressService.update(e.getId(), e);
        Message msg = new Message();
        if(result){
            msg.setStatus(1);
            msg.setResult("更新成功");
        }else{
            msg.setStatus(-1);
            msg.setResult("更新失败");
        }
        return JSONUtil.toJson(msg);
    }
}
